package my.mypool.core.http;

import my.mypool.core.PoolablePool;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpState;

import java.util.Map;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-9-21
 * Time: 10:37:17
 * To change this template use File | Settings | File Templates.
 */
public class HttpClient {
    private PoolablePool pool = null;
    public HttpClient(String host, int port,int maxIdle, int initIdleCapacity) throws Exception {
         pool = new PoolablePool(PoolableHttp.class, host + ":" + port,maxIdle,initIdleCapacity);
    }

    public String doGet(String path, Map<String, String> params, Map<String, String> headers) throws Exception {
        GetMethod method = new GetMethod();
        HttpMethodParams httpParams = new HttpMethodParams();
        httpParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        if (params != null) {
            String[] keys = params.keySet().toArray(new String[0]);
            for (int i = 0; i < keys.length; i++) {
                httpParams.setParameter(keys[i], params.get(keys[i]));
            }
        }
        method.setParams(httpParams);
        if (headers != null) {
            String[] keys = headers.keySet().toArray(new String[0]);
            for (int i = 0; i < keys.length; i++) {
                method.setRequestHeader(keys[i], headers.get(keys[i]));
            }
        }
        if (!path.startsWith("/")) path = "/" + path;
        method.setPath(path);
        PoolableHttp poolableHttp = pool.borrowObject();
        HttpConnection conn = poolableHttp.getHttpConnection();
        try {
            method.execute(new HttpState(), conn);
            return method.getResponseBodyAsString();
        } catch (Exception ex) {
            throw ex;
        } finally {
            pool.returnObject(poolableHttp);
        }
    }

    public String doPut(String path, String value, Map<String, String> params, Map<String, String> headers) throws Exception {
        PutMethod method = new PutMethod();
        HttpMethodParams httpParams = new HttpMethodParams();
        httpParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        if (params != null) {
            String[] keys = params.keySet().toArray(new String[0]);
            for (int i = 0; i < keys.length; i++) {
                httpParams.setParameter(keys[i], params.get(keys[i]));
            }
        }
        method.setParams(httpParams);
        if (headers != null) {
            String[] keys = headers.keySet().toArray(new String[0]);
            for (int i = 0; i < keys.length; i++) {
                method.setRequestHeader(keys[i], headers.get(keys[i]));
            }
        }
        if (!path.startsWith("/")) path = "/" + path;
        method.setPath(path);
        method.setRequestBody(value);
        PoolableHttp poolableHttp = pool.borrowObject();
        HttpConnection conn = poolableHttp.getHttpConnection();
        try {
            method.execute(new HttpState(), conn);
            return method.getResponseBodyAsString();
        } catch (Exception ex) {
            throw ex;
        } finally {
            pool.returnObject(poolableHttp);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        for(int i=0;i<100;i++){
             HttpConnection conn=new HttpConnection("192.168.21.141",11211);
            conn.open();
            conn.close();
        }

        while(true){
            Thread.sleep(10000);
        }
    }
}
